package com.maple.common.widget.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.Button
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import com.maple.common.R
import com.maple.utils.ext.layoutInflater
import com.maple.utils.ext.toGone
import com.maple.utils.ext.toVisible
import com.maple.utils.util.LogUtils
import com.maple.utils.util.ToastUtils

class NumControllerLayout: LinearLayout {
    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        initView(
            context,
            attributeSet,
            defStyleAttr
        )
    }

    private var llController: LinearLayout? = null
    private var ibtnAdd: ImageButton? = null
    private var ibtnSub: ImageButton? = null
    private var tvNum: TextView? = null

    private var listener: OnNumControllerListener? = null

    private var num: Int = 1

    fun setOnNumControllerListener(l: OnNumControllerListener?) {
        l?.let {
            this.listener = l
        }
    }

    private fun initView(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int = 0) {
        val view: View = context.layoutInflater.inflate(R.layout.layout_num_controller, this)

//        attributeSet?.let {
//            val typedArray: TypedArray = context.obtainStyledAttributes(it, R.styleable.PaginationLayout)
//            typedArray.let { ta ->
//                ta.recycle()
//            }
//        }

        llController = view.findViewById(R.id.ll_num_controller)
        ibtnAdd = view.findViewById(R.id.ibtn_add)
        ibtnAdd?.setOnClickListener {
            if(num > 1000) {
                ToastUtils.showToast("已经是最大数量！")
                return@setOnClickListener
            }
            num ++
            setNum()
            listener?.onAdd(num)
        }
        ibtnSub = view.findViewById(R.id.ibtn_sub)
        ibtnSub?.setOnClickListener {
            if(num <= 1) {
                ToastUtils.showToast("已经是最小数量！")
                return@setOnClickListener
            }
            num --
            setNum()
            listener?.onSub(num)
        }
        tvNum = view.findViewById(R.id.tv_num)
        setNum()
    }


    fun onShowController(isShow: Boolean) {
        llController?.let {
            if(isShow) {
                it.toVisible()
            } else {
                it.toGone()
            }
        }
    }

    private fun setNum() {
        tvNum?.text = "${num}"
    }

    fun getNum():Int = num

    interface OnNumControllerListener {

        fun onAdd(num: Int) {
            ToastUtils.showToast("add :${num}")
        }
        fun onSub(num: Int) {
            ToastUtils.showToast("sub :${num}")
        }
    }


}